<?php


namespace app\admin\validates;


use think\Validate;

class PractiseValidate extends Validate
{
    /**
     * 验证规则
     * @var array
     */
    protected $rule = [
        'id' => 'require|number',
        'name' => 'chsDash|max:30',
        'special_id' => 'require|number',
        'desc' => 'chsDash',
        'start_time' => 'require|date',
        'end_time' => 'require|date',
        'time' => 'require|number',
        'pass_score' => 'require|number',
        'img' => 'require|max:255',
        'sort' => 'number',
    ];

    /**
     * 验证字段描述
     * @var string[]
     */
    protected $field = [
        'id' => 'ID',
        'name' => '练习名称',
        'special_id' => '专业',
        'desc' => '简介',
        'start_time' => '开始时间',
        'end_time' => '结束时间',
        'time' => '练习时长',
        'pass_score' => '及格分数',
        'img' => '封面图片',
        'sort' => '排序',
    ];

    /**
     * 验证场景
     * @var array
     */
    protected $scene = [
        'add' => ['name', 'special_id','desc','start_time','end_time','time','pass_score'],
        'edit' => ['id', 'name', 'special_id','desc','start_time','end_time','time','pass_score'],
        'del' => ['id'],
        'details' => ['id']
    ];

    /**
     * 验证手机号格式
     * @param string $value
     * @param string $rule
     * @param array $data
     * @return bool|string
     */
    protected function checkMobile(string $value, string $rule, array $data)
    {
        if (isset($data['area_code']) && $data['area_code'] === '+86') {
            $preg = '/^1[3-9]\d{9}$/';
            if (!preg_match($preg, $value)) {
                return '手机号码格式错误';
            }
        }
        return true;
    }
}